package com.zh.search.job.cycle;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.zh.search.dao.ArticleEsDao;
import com.zh.search.dao.VideoEsDao;
import com.zh.search.domain.dto.ArticleEsDto;
import com.zh.search.domain.dto.VideoEsDto;
import com.zh.web.domain.media.vo.ArticleVo;
import com.zh.web.domain.media.vo.VideoVo;
import com.zh.web.openApi.ArticleClient;
import com.zh.web.openApi.VideoClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 增量同步文章到 es
 */
@Component
@Slf4j
public class IncSyncArticleToEs {

    private final int pageSize = 500;
    private final ArticleClient articleClient;

    private final ArticleEsDao articleEsDao;

    private final VideoClient videoClient;
    private final VideoEsDao videoEsDao;

    public IncSyncArticleToEs(ArticleClient articleClient, ArticleEsDao articleEsDao, VideoClient videoClient, VideoEsDao videoEsDao) {
        this.articleClient = articleClient;
        this.articleEsDao = articleEsDao;
        this.videoClient = videoClient;
        this.videoEsDao = videoEsDao;
    }


    /**
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 60 * 1000)
    public void run() {
        syncArticleToEs();
        syncVideoToEs();
    }

    private void syncArticleToEs() {
        // 查询近 5 分钟内的数据
        LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
        List<ArticleVo> articleVoList = articleClient.listArticleAfterTime(fiveMinutesAgo).getData();
        if (CollUtil.isEmpty(articleVoList)) {
            log.info("没有需要同步的文章数据");
            return;
        }
        List<ArticleEsDto> articleEsDtoList = articleVoList.stream()
                .map(articleVo -> BeanUtil.copyProperties(articleVo, ArticleEsDto.class)).collect(Collectors.toList());
        int total = articleEsDtoList.size();
        log.info("开始同步文章到es, total {}", total);
        for (int i = 0; i < total; i += pageSize) {
            int end = Math.min(i + pageSize, total);
            log.info("同步文章 from {} to {}", i, end);
            articleEsDao.saveAll(articleEsDtoList.subList(i, end));
        }
        log.info("同步文章完成");
    }

    private void syncVideoToEs() {
        // 查询近 5 分钟内的数据
        LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
        List<VideoVo> videoVoList = videoClient.listArticleAfterTime(fiveMinutesAgo).getData();
        if (CollUtil.isEmpty(videoVoList)) {
            log.info("没有需要同步的视频数据");
            return;
        }
        List<VideoEsDto> videoEsDtoList = videoVoList.stream()
                .map(videoVo -> BeanUtil.copyProperties(videoVo, VideoEsDto.class)).collect(Collectors.toList());
        int total = videoEsDtoList.size();
        log.info("开始同步视频到es, total {}", total);
        for (int i = 0; i < total; i += pageSize) {
            int end = Math.min(i + pageSize, total);
            log.info("同步视频 from {} to {}", i, end);
            videoEsDao.saveAll(videoEsDtoList.subList(i, end));
        }
        log.info("同步视频完成");
    }
}
